﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;
using InfoJetSoft.SharePointPeople;

//CID 0656 //2010.10.17 //Begin
namespace InfoJetSoft.Service.Worker
{
    class PNGSearchPrincipalsWorker
    {
        public static string searchPrincipals(Form form)
        {
            string ajaxBracket = FormUtil.getAjaxBracket(form);
            
            StringBuilder buffer = new StringBuilder();
            buffer.Append(ajaxBracket);
            string parent = "";
            if (ConfigUtil.UseAJAX())
            {
                parent = "";
            }
            else
            {
                buffer.Append("<script>");
                parent = "parent.";
            }
            buffer.Append(parent + "InfoJet_StopProgress();");
            buffer.Append(parent + "InfoJet_Postbacking = 0;");
            buffer.Append(parent + "InfoJet_OnPostbackEnd();");

            try
            {
                string formId = form.HttpContext.Request.Params["xdoc_param_form_id"];
                string pngId = form.HttpContext.Request.Params["xdoc_param_png_id"];
                string searchText = form.HttpContext.Request.Params["xdoc_param_png_search_text"];

                //CID 0733 //2010.12.18 //Begin
                PNGInfo pngInfo = form.Manifest.PNGInfoTable[pngId] as PNGInfo;
                //CID 0733 //2010.12.18 //End

                People people = new People();
                //CID 0701 //2010.11.08 //Begin
                people.Credentials = System.Net.CredentialCache.DefaultCredentials;
                //System.Net.CredentialCache cache = new System.Net.CredentialCache();
                //cache.Add(new Uri("http://win2003ssl:82"), "NTLM", new System.Net.NetworkCredential("Administrator", "123", "win2003ssl"));
                //people.Credentials = cache;
                //CID 0733 //2010.12.18 //Begin
                //CID 0914 //2011.05.20 //Begin
                string server = pngInfo.Server;
                if (form.Manifest.SolutionFormatVersion.StartsWith("2."))
                {
                    if (ConfigUtil.EnabledSessionCache())
                    {
                        FormSession.restoreDataTable(form);
                        XmlDocument contextDoc = form.DataTable["Context"] as XmlDocument;
                        if (contextDoc != null)
                        {
                            server = contextDoc.DocumentElement.GetAttribute("siteUrl");
                        }
                    }
                }
                //CID 0914 //2011.05.20 //End
                //CID 0733 //2010.12.18 //End
                if (server.Equals("http://"))
                {
                    server = PathUtil.getBasePath(form.HttpContext);
                }
                if (!server.ToLower().EndsWith("/_vti_bin/People.asmx"))
                {
                    if (server.EndsWith("/"))
                    {
                        server = server + "_vti_bin/People.asmx";
                    }
                    else
                    {
                        server = server + "/_vti_bin/People.asmx";
                    }
                }
                people.Url = server;
                //CID 0701 //2010.11.08 //End
                SPPrincipalType searchType = SPPrincipalType.None;
                if (pngInfo.SearchPeopleOnly.Equals("true"))
                {
                    searchType = SPPrincipalType.User;
                }
                else
                {
                    searchType = SPPrincipalType.User | SPPrincipalType.SharePointGroup | SPPrincipalType.SecurityGroup | SPPrincipalType.DistributionList;
                }
                PrincipalInfo[] principals = people.SearchPrincipals(searchText, 200, searchType);
                buffer.Append("var InfoJet_Principals = new Array();");
                int index = 0;
                foreach (PrincipalInfo principal in principals)
                {
                    buffer.Append(parent + "InfoJet_Principals[" + index + "] = { ");
                    buffer.Append(parent + "DisplayName: '" + principal.DisplayName.Replace("\\", "\\\\") + "', ");
                    buffer.Append(parent + "AccountName: '" + principal.AccountName.Replace("\\", "\\\\") + "', ");
                    buffer.Append(parent + "PrincipalType: '" + principal.PrincipalType + "' ");
                    buffer.Append(parent + "};\r\n");
                    index++;
                }
                buffer.Append(parent + "InfoJet_CurrentDialog.EnableFindButton();");
                buffer.Append(parent + "InfoJet_ShowPNGPrincipals( InfoJet_Principals );");
            }
            catch (Exception e)
            {
                buffer.Append(parent + "InfoJet_CurrentDialog.EnableFindButton();");
                buffer.Append(parent + "alert(\"" + e.Message + "\");");
            }

            if (!ConfigUtil.UseAJAX())
            {
                buffer.Append("</script>");
            }

            buffer.Append(ajaxBracket);
            return buffer.ToString();
        }
    }
}
//CID 0656 //2010.11.08 //End